$(function () {
    var defaultCell = "defaultCell";
    var playerACell = "playerACell";
    var playerBCell = "playerBCell";
    var startCell = "startCell";
    var endCell = "endCell";

    function GetDefaultCell(id) {
        var res = '<div id="' + id + '" class="leftFloat cell ' + defaultCell + '">A</div>';
        return res;
    }

    function GetPlayerACell(id) {
        var res = '<div id="' + id + '" class="leftFloat cell ' + playerACell + '">A</div>';
        return res;
    }

    function GetPlayerBCell(id) {
        var res = '<div id="' + id + '" class="leftFloat cell ' + playerBCell + '">A</div>';
        return res;
    }

    function GetClearDiv() {
        var res = '<div class="clearFloat"></div>';
        return res;
    }

    function BuildID(row, col) {
        var id = "row_" + row + "_col_" + col;
        return id;
    }

    function BuildGameTable() {
        var content = "";
        $("#gameTable").html('');

        for (var row = 0; row < 5; row++) {
            for (var col = 0; col < 10; col++) {
                content += GetDefaultCell(BuildID(row, col));
            }
            content += GetClearDiv();
        }

        $("#gameTable").html(content);
        $(".cell").bind('click', CellClick);
        $(".cell").bind('mouseover', CellHoverIn);
        $(".cell").bind('mouseout', CellHoverOut);
    }

    var cellBorderColor = "";

    function IsFirstClick() {
        var startID = $("." + startCell).attr("id");
        if (startID == undefined) {
            return true;
        }
        return false;
    }

    function CellClick() {
        var currentID = $(this).attr("id");

        var isPosible = IfPathPosibleCreate(this);
        if (isPosible == true) {
            return;
        }

        var isFirstClick = IsFirstClick();
        if (isFirstClick == true) {
            CreateFirstClick(currentID);
        }
    }

    function CreateFirstClick(currentID) {
        RemoveStartCell();
        AddStartCell(currentID);
    }

    function CellHoverIn() {
        cellBorderColor = $(this).css("border-color");
        $(this).css("border-color", "#000000");
        $(this).css("color", "#FFF304");
        ShowPreview(this);
    };

    function CellHoverOut() {
        $(this).css("border-color", cellBorderColor);
        $(this).css("color", "#FFFFFF");
        //HidePreview(this);
    };

    BuildGameTable();

    function GetStartArray() {
        var startID = $("." + startCell).attr("id");

        if (startID == undefined || startID == null) {
            return new Array();
        }
        var startArray = startID.split("_");
        return startArray;
    }

    function GetEndArray(obj) {
        var endID = $(obj).attr("id");
        var endArray = endID.split("_");
        return endArray;
    }

    function IfPathPosibleCreate(obj) {
        var startArray = GetStartArray();
        var startRow = parseInt(startArray[1]);
        var startCol = parseInt(startArray[3]);

        var endArray = GetEndArray(obj);
        var endRow = parseInt(endArray[1]);
        var endCol = parseInt(endArray[3]);

        var isHorizontalPathPosible = CheckIsPathPosible(startRow, startCol, endRow, endCol);
        //var isVerticalPathPosible = CheckIsPathPosible(startRow, startCol, endRow + 2, endCol);
        //var isDiagonalPathPosible = CheckIsPathPosible(startRow, startCol, endRow + 2, endCol + 2);
        console.log(endRow + " " + endCol);

        if (isHorizontalPathPosible == true) {
            AddHorizontalPreview(startRow, startCol);
        }

        //        if (isVerticalPathPosible == true) {
        //            AddVerticalPreview(row, col);
        //        }

        //        if (isDiagonalPathPosible == true) {
        //            AddDiagonalPreview(row, col);
        //        }
    }

    function ShowPreview(obj) {
        var startArray = GetStartArray();
        var startRow = parseInt(startArray[1]);
        var startCol = parseInt(startArray[3]);

        //        var endArray = GetEndArray(obj);
        //        var endRow = parseInt(endArray[1]);
        //        var endCol = parseInt(endArray[3]);

        //        var isHorizontalPathPosible = CheckIsPathPosible(startRow, startCol, endRow, endCol);
        //        var isVerticalPathPosible = CheckIsPathPosible(startRow, startCol, endRow, endCol);
        //        var isDiagonalPathPosible = CheckIsPathPosible(startRow, startCol, endRow, endCol);

        //        if (isHorizontalPathPosible == true) {
        //            AddHorizontalPreview(row, col);
        //        }

        //        if (isVerticalPathPosible == true) {
        //            AddVerticalPreview(row, col);
        //        }

        //        if (isDiagonalPathPosible == true) {
        //            AddDiagonalPreview(row, col);
        //        }
    }

    function HidePreview(obj) {
        var currentID = $(obj).attr("id");
        var array = currentID.split("_");
        var row = parseInt(array[1]);
        var col = parseInt(array[3]);

        RemoveHorizontalPreview(row, col);
        RemoveVerticalPreview(row, col);
        RemoveDiagonalPreview(row, col);
    }

    function AddStartCell(id) {
        $("#" + id).removeClass(defaultCell);
        $("#" + id).addClass(playerACell);
        $("#" + id).addClass(startCell);
    }

    function AddPlayerACell(id) {
        $("#" + id).addClass(playerACell);
        $("#" + id).addClass(startCell);
    }

    function RemoveStartCell() {
        $("." + startCell).removeClass(playerACell);
        $("." + startCell).addClass(defaultCell);
        //$("." + startCell).addClass(playerACell);
        //        $("#" + id).removeClass(playerACell);
        //        $("#" + id).addClass(defaultCell);
    }

    function CheckIsPathPosible(startRow, startCol, endRow, endCol) {
        var startId = BuildID(startRow, startCol);
        var isDefaultCurrent = $("#" + startId).hasClass(startCell);

        var endId = BuildID(endRow, endCol);
        var isDefaultFirst = $("#" + endId).hasClass(defaultCell);

        if (isDefaultCurrent == true && isDefaultFirst == true) {
            return true;
        }
        return false;
    }

    function AddHorizontalPreview(row, col) {
        var firstID = BuildID(row, col);
        var secondID = BuildID(row, col + 1);
        var thirdID = BuildID(row, col + 2);

        console.log(firstID + ", " + secondID + ", " + thirdID + ", ");

        AddPlayerACell(firstID);
        AddPlayerACell(secondID);
        AddPlayerACell(thirdID);
    }

    function AddVerticalPreview(row, col) {
        var firstID = BuildID(row, col);
        var secondID = BuildID(row + 1, col);
        var thirdID = BuildID(row + 2, col);

        AddPreviewClass(firstID);
        AddPreviewClass(secondID);
        AddPreviewClass(thirdID);
    }

    function AddDiagonalPreview(row, col) {
        var firstID = BuildID(row, col);
        var secondID = BuildID(row + 1, col + 1);
        var thirdID = BuildID(row + 2, col + 2);

        AddPreviewClass(firstID);
        AddPreviewClass(secondID);
        AddPreviewClass(thirdID);
    }

    function RemoveHorizontalPreview(row, col) {
        var firstID = BuildID(row, col);
        var secondID = BuildID(row, col + 1);
        var thirdID = BuildID(row, col + 2);

        RemovePreviewClass(firstID);
        RemovePreviewClass(secondID);
        RemovePreviewClass(thirdID);
    }

    function RemoveVerticalPreview(row, col) {
        var firstID = BuildID(row, col);
        var secondID = BuildID(row + 1, col);
        var thirdID = BuildID(row + 2, col);

        RemovePreviewClass(firstID);
        RemovePreviewClass(secondID);
        RemovePreviewClass(thirdID);
    }

    function RemoveDiagonalPreview(row, col) {
        var firstID = BuildID(row, col);
        var secondID = BuildID(row + 1, col + 1);
        var thirdID = BuildID(row + 2, col + 2);

        RemovePreviewClass(firstID);
        RemovePreviewClass(secondID);
        RemovePreviewClass(thirdID);
    }

});
